package net.sourceforge.krama.tag.panel;

import java.io.IOException;

import javax.servlet.jsp.PageContext;

import net.sourceforge.krama.css.CSSGlobalPanel;
import net.sourceforge.krama.css.CSSTemplate;
import net.sourceforge.krama.css.child.Background;
import net.sourceforge.krama.html.child.HTMLGlobalPanel;
import net.sourceforge.krama.html.child.HTMLLabel;
import net.sourceforge.krama.tag.T;
import net.sourceforge.krama.tag.widget.AbstractT;
import net.sourceforge.krama.util.TUtil;


public class TGlobalPanel extends T {

	private CSSGlobalPanel cssPanel = new CSSGlobalPanel();
	private HTMLGlobalPanel htmlPanel = new HTMLGlobalPanel();
	private PageContext context;
	private String html = "";
	public static String name;
	private String id;
	private String htmlChild = "";
	private String cssChild = "";
	
	private String margin;
	private String padding;
	private String height;
	private String width;
	private Background background;
	private String style;
	
	private boolean drag = false;
	
	public TGlobalPanel(PageContext context, String name, String id) {
		this.context = context;
		this.name = "krama-" + name;
		htmlPanel.setName(this.name);
		this.id = id;
		htmlPanel.setId("krama-"+this.id);
	}

	public void add(AbstractT widget) throws IOException {

		widget.setParentName(name);
		widget.draw();
		this.htmlChild += widget.getHtml();
		this.cssChild += widget.getCSS();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = "krama-" + name;
		htmlPanel.setName(this.name);
	}

	@Override
	public void draw() {

		this.html = htmlPanel.starDraw();
		this.html += htmlChild;
		this.html += htmlPanel.endDraw();
		setContext(context);
		try {
			write(html);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		doStartTag();
	}

	@Override
	public void setParentName(String parentName) {
		// TODO Auto-generated method stub
	}

	@Override
	public String getHtml() {
		// TODO Auto-generated method stub
		return html;
	}

	@Override
	public String getCSS() {
		// TODO Auto-generated method stub
		if (name != null) {
			return cssPanel.getCSS("." + name);
		} else
			return "";
	}

	public String show() {
		draw();
		return getCSS() + cssChild;
	}

	public void setMargin(String margin) {
		this.margin = margin;
		cssPanel.setMargin(margin);
	}

	public void setPadding(String padding) {
		this.padding = padding;
		cssPanel.setPadding(padding);
	}

	public void setHeight(String height) {
		this.height = height;
		cssPanel.setHeight(height);
	}

	public void setWidth(String width) {
		this.width = width;
		cssPanel.setWidth(width);
	}
	
	public void setMargin(int margin) {
		this.margin = String.valueOf(margin)+"px";
		cssPanel.setMargin(this.margin);
	}
	
	public void setPadding(int padding) {
		this.padding = String.valueOf(padding)+"px";
		cssPanel.setPadding(this.padding);
	}

	public void setHeight(int height) {
		this.height = String.valueOf(height)+"px";
		cssPanel.setHeight(this.height);
	}
	
	public void setWidth(int width) {
		this.width = String.valueOf(width)+"px";
		cssPanel.setWidth(this.width);
	}

	public void setBackground(Background background) {
		this.background = background;
		cssPanel.setBackground(background.getBackground());
	}
	
	public void setStyle(String style) {
		this.style = style;
		cssPanel.setStyle(style);
	}
	
	public String getMargin() {
		return margin;
	}

	public String getPadding() {
		return padding;
	}

	public String getHeight() {
		return height;
	}

	public String getWidth() {
		return width;
	}

	public Background getBackground() {
		return background;
	}

	public String getStyle() {
		return style;
	}

	public void setDrag(boolean drag) {
		
		this.drag = drag;
		htmlPanel.setDrag(true);
		
		TUtil.javascriptShouldBeAvailable = true;
		TUtil.domDragShouldBeAvailable = true;
		
		cssPanel.setStyle((style!=null)?style:""+" position:relative;");
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		if (id != null) {
			this.id = id;
			htmlPanel.setId("krama-"+this.id);
		}
	}
}
